﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class WorktimePropertyDAO : BaseDAO
    {
        public WorktimePropertyDAO()
            : base ()
        {
        }

        public BLWorktimeProperty search(BLWorktimeProperty filter)
        {
            BLWorktimeProperty result = new BLWorktimeProperty(filter.AgentId, filter.StartTime);
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@agentId", filter.AgentId,ParameterDirection.Input));
            condition.Add(new BaseCondition("@startTime", filter.StartTime, ParameterDirection.Input));
            DataTable data = select("usp_SearchWorktimes", condition);
            if (data.Rows.Count > 0)
            {
                result.Id = (Guid)data.Rows[0]["WorktimePropertyId"];
                result.AgentId = (Guid)data.Rows[0]["AgentId"];
                result.Description = (string)data.Rows[0]["Description"];
                result.StartTime = (DateTime)data.Rows[0]["StartTime"];
                result.Approved = (short)data.Rows[0]["IsApproved"];
                result.DataSignature = (long)data.Rows[0]["DataSignature"];
                result.UpdateDate = (DateTime)data.Rows[0]["UpdateDate"];
                result.UpdateUserId = (Guid)data.Rows[0]["UpdateUserId"];
            }
            return result;
        }

        public int insert(BLWorktimeProperty property)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", Guid.NewGuid(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", property.AgentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startTime", property.StartTime, ParameterDirection.Input));
            condition.Add(new BaseCondition("@isApproved", property.Approved, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", property.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature",property.DataSignature,ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode",errorCode,ParameterDirection.Output));
            int result = update("usp_AddWorktimeProperty", condition);
            return result;
        }

        public int update(BLWorktimeProperty property)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", property.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@isApproved", property.Approved, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", property.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", property.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateWorktimeProperty", condition);
            return result;
        }

        public int delete(BLWorktimeProperty property)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", property.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", property.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteWorktimeProperty", condition);
            return result;
        }
    }
}
